package com.ellricky.newtv

import android.content.pm.ActivityInfo
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import androidx.viewpager2.widget.ViewPager2
import com.andr.common.ui.activity.BaseActivity
import com.andr.common.ui.mvp.BasePresenter
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEListCallback
import com.ellricky.baselib.util.jump
import com.ellricky.newtv.adapter.CourseTypeAdapter
import com.ellricky.newtv.adapter.CourseTypePagerAdapter
import com.ellricky.newtv.bean.TvCourseType
import com.ellricky.newtv.const.TvConst
import com.ellricky.newtv.utils.CacheToken
import kotlinx.android.synthetic.main.activity_new_tv_course_type.*

class NewTvCourseTypeActivity : BaseActivity<Void, BasePresenter<Void>>() {

    private val courseTypeAdapter by lazy {
        CourseTypePagerAdapter()
    }
    private var currentPosition: Int = 0

    override fun setOrientation() {
        this.requestedOrientation =
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
    }

    override fun getContentLayout(): Int {
        return R.layout.activity_new_tv_course_type
    }

    override fun initUIView() {
        CacheToken.verifyToken()
        tv_back.setOnClickListener(this)
        iv_left.setOnClickListener(this)
        iv_right.setOnClickListener(this)
        initRv()
        loadData()
    }

    private fun loadData() {
        val url = EAppUtil.getTvApiHost()
        val paramMap = mutableMapOf(
            "action" to "courseTypeList",
            "eid" to LocalCacheUtil.getTvUid()
        )
        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEListCallback<TvCourseType>(this, false) {
                override fun onSuccess(data: MutableList<TvCourseType>, msg: String?) {
                    tv_course_number.text = "我订阅的课程类型 共${data.size}个"
                    val list = averageAssign(data, 4)
                    courseTypeAdapter.setCourseTypeList(list)
                    if (list.size > 1) {
                        iv_right.visibility = View.VISIBLE
                    }
                }
            }, this)
    }

    private fun <T> averageAssign(source: List<T>, size: Int): List<List<T>> {
        val result = ArrayList<List<T>>()
        var low = 0
        var high = size
        val number = source.size / size
        for (index in 0..number + 1) {
            if (high < source.size) {
                val list = source.subList(low, high)
                result.add(list)
            } else {
                val list = source.subList(low, source.size)
                result.add(list)
                break
            }
            low += (index + 1) * size
            high += (index + 1) * size
        }
        return result
    }

    override fun onClickView(v: View) {
        super.onClickView(v)
        when (v.id) {
            R.id.tv_back -> finish()
            R.id.iv_left -> vp_course_type.setCurrentItem(currentPosition - 1, true)
            R.id.iv_right -> vp_course_type.setCurrentItem(currentPosition + 1, true)
        }
    }

    private fun initRv() {
        vp_course_type.adapter = courseTypeAdapter
        vp_course_type.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                currentPosition = position
                val itemCount = courseTypeAdapter.itemCount
                if (position == 0 && position < itemCount - 1) {
                    iv_left.visibility = View.GONE
                } else {
                    iv_left.visibility = View.VISIBLE
                }

                if (position == itemCount - 1) {
                    iv_right.visibility = View.GONE
                } else {
                    iv_right.visibility = View.VISIBLE
                }
            }
        })
    }
}